package com.tanhua.server.controller;

import com.tanhua.domain.mongo.Publish;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.CommentService;
import com.tanhua.server.service.MovementsMqService;
import com.tanhua.server.service.MovementsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/movements")
public class MovementsController {

    @Autowired
    private MovementsService movementsService;

    @Autowired
    private MovementsMqService movementsMqService;

    @Autowired
    private CommentService commentService;

    /**
     * 发布动态
     *  POST  /movements
     *  请求参数
     *    图片数据，不能用json
     *    参数：
     *      1、发布对象publish
     *      2、文件数组 imageContent数组
     */
    @PostMapping
    public ResponseEntity saveMovements(Publish publish, MultipartFile [] imageContent) throws IOException {
        //保存用户id
        publish.setUserId(UserHolder.getUserId());
        //业务操作
        ResponseEntity entity = movementsService.saveMovements(publish, imageContent);
        //发送消息
        movementsMqService.publishMsg(publish.getId().toHexString());
        return entity;
    }

    /**
     * 查询好友动态
     *  GET/movements
     *  请求参数:
     *      Query
     *         int page,int pagesize
     *  查询：
     *      PageResult
     *   vo对象
     */
    @GetMapping
    public ResponseEntity queryPublishList(@RequestParam(defaultValue = "1") Integer page,
                                           @RequestParam(defaultValue = "10") Integer pagesize) {
        //前段有一个bug，page = 0
        page = page<1? 1: page;
        return this.movementsService.queryUserPublishList(page, pagesize);
    }

    /**
     * 查询推荐动态
     */
    @GetMapping("/recommend")
    public ResponseEntity queryRecommendPublishList(@RequestParam(defaultValue = "1") Integer page,
                                           @RequestParam(defaultValue = "10") Integer pagesize) {
        //前段有一个bug，page = 0
        page = page<1? 1: page;
        return this.movementsService.queryRecommendPublishList(page, pagesize);
    }


    /**
     * 查询指定用户的动态列表
     *  参数：userId
     */
    @GetMapping("/all")
    public ResponseEntity queryAlbumList(@RequestParam(defaultValue = "1") Integer page,
                                         @RequestParam(defaultValue = "10") Integer pagesize,
                                         Long userId) {
        page = page<1? 1: page;
        return this.movementsService.queryAlbumList(page, pagesize,userId);
    }

    /**
     * 对动态，点赞
     *      请求连接：GET   /:id/like
     *      参数：地址参数 id
     */
    @GetMapping("/{id}/like")
    public ResponseEntity likeComment(@PathVariable("id") String publishId) {
        movementsMqService.likePublishMsg(publishId);
        return commentService.likeComment(publishId);
    }

    /**
     * 对动态取消点赞
     *      请求连接：GET   /:id/dislike
     *      参数：地址参数 id
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity dislikeComment(@PathVariable("id") String publishId) {
        movementsMqService.disLikePublishMsg(publishId);
        return commentService.dislikeComment(publishId);
    }


    /**
     * 喜欢
     */
    @GetMapping("/{id}/love")
    public ResponseEntity<Long> loveComment(@PathVariable("id") String publishId) {
        movementsMqService.lovePublishMsg(publishId);
        return this.commentService.loveComment(publishId);
    }

    /**
     * 取消喜欢
     */
    @GetMapping("/{id}/unlove")
    public ResponseEntity<Long> unLoveComment(@PathVariable("id") String publishId) {
        movementsMqService.disLovePublishMsg(publishId);
        return this.commentService.unLoveComment(publishId);
    }

    /**
     * 根据动态id，查询动态详情
     *  请求连接：GET  /:id
     */
    @GetMapping("/{id}")
    public ResponseEntity queryMovementsById(@PathVariable("id") String publishId) {
        return movementsService.queryMovementsById(publishId);
    }

    /**
     * 查看来访记录
     *  请求连接：GET   /visitors
     */
    @GetMapping("/visitors")
    public ResponseEntity visitors() {
        return movementsService.queryVisitors();
    }
}
